package cn.tj.service.impl;

import cn.tj.entity.Register;
import cn.tj.entity.User;
import cn.tj.mapper.UserMapper;
import cn.tj.service.UserService;
import cn.tj.util.Md5Util;
import cn.tj.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByAccount(String account) {
        return userMapper.findByAccount(account);
    }

    @Override
    public void register(User user) {
        String password = Md5Util.getMD5String(user.getPassword());
        userMapper.addRegister(user.getAccount(),password);
    }

    @Override
    public User userInfo() {
        //从ThreadLocalUtil获取
        Map<String,Object> map = ThreadLocalUtil.get();
        String account =(String) map.get("account");
        User byAccount = userMapper.findByAccount(account);
        return byAccount;
    }

    @Override
    public Register findRegisterByAccount(String account) {
        return userMapper.findRegisterByAccount(account);
    }

    @Override
    public void editInfo(User user) {
        userMapper.editInfo(user);
    }

    @Override
    public void editPassword(String account,String password) {
        userMapper.editPassword(Md5Util.getMD5String(password),account);
    }

    @Override
    public void editAvatar(String avatar) {
        Map<String,Object> map = ThreadLocalUtil.get();
        String account =(String) map.get("account");
        //通过账号名查询
        User byAccount = userMapper.findByAccount(account);
        userMapper.editAvatar(avatar,byAccount.getId());
    }
}
